package com.easy.web.service.impl;

import com.easy.web.mapper.ArticleMapper;
import com.easy.web.model.Article;
import com.easy.web.model.ArticleExample;
import com.easy.web.service.ArticleService;
import com.easy.web.utils.DateUtil;
import com.easy.web.utils.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2017/1/4.
 */
@Service
@Transactional
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    /**
     * 查询最新的文章
     */
    @Override
//    @Cacheable(value = "Article",key = "#limitNum")
    public List<Article> getArticleList(String limitNum, String type) {

        ArticleExample articleExample=new ArticleExample();
        ArticleExample.Criteria criteria=articleExample.createCriteria();
        if(StringUtil.isNotEmpty(type)){
            criteria.andArticleTypeEqualTo(type);
        }
        String cla="create_time desc";
        if(StringUtil.isNotEmpty(limitNum)){
            cla=cla+" limit "+limitNum;
        }
        articleExample.setOrderByClause(cla);
        return articleMapper.selectByExample(articleExample);
    }

    /**
     * 新增一篇文章
     *
     * @param article
     */
    @Override
//    @CachePut(value = "Article",key = "#id")
    public void addArticle(Article article) {
        articleMapper.insert(article);
    }

    /**
     * 编辑当前文章
     *
     * @param article
     */
    @Override
//    @CachePut(value = "Article",key = "#id")
    public void updateArticle(Article article) {
        articleMapper.updateByPrimaryKeySelective(article);
    }


    /**
     * 根据id查询文章信息
     *
     * @param id
     */
    @Override
//    @Cacheable(value = "Article",key = "#id")
    public Article queryArticleById(String id) {
        return articleMapper.selectByPrimaryKey(id);
    }

    /**
     * 删除某个文章
     *
     * @param id
     */
    @Override
//    @CacheEvict(value = "Article",key = "#id")
    public void delArticle(String id) {
        articleMapper.deleteByPrimaryKey(id);
    }


    /**
     * 查询某一类文章
     *
     * @param arJson
     */
    @Override
    public List<Article> getArticlesByExample(JSONObject arJson) {
        ArticleExample articleExample=new ArticleExample();
        ArticleExample.Criteria criteria=articleExample.createCriteria();
        String startTime=arJson.optString("searchTacticsDateStart");
        if(StringUtil.isNotEmpty(startTime)){
            criteria.andCreateTimeGreaterThanOrEqualTo(DateUtil.parseDatePaf(startTime));
        }
        String endTime=arJson.optString("searchTacticsDateStop");
        if(StringUtil.isNotEmpty(endTime)){
            criteria.andCreateTimeLessThanOrEqualTo(DateUtil.parseDatePaf(endTime));
        }
        String type=arJson.optString("searchTacticsType");
        if(StringUtil.isNotEmpty(type)){
            criteria.andArticleTypeEqualTo(type);
        }
        String title=arJson.optString("searchTacticsTitle");
        if(StringUtil.isNotEmpty(title)){
            criteria.andArticleTitleLike("%"+title+"%");
        }
        String author=arJson.optString("searchTacticsAuthor");
        if(StringUtil.isNotEmpty(author)){
            criteria.andArticleAuthorLike("%"+author+"%");
        }
        String publisher=arJson.optString("searchTacticsPublisher");
        if(StringUtil.isNotEmpty(publisher)){
            criteria.andAuthorPublisherLike(""+publisher+"%");
        }
        articleExample.setOrderByClause("create_time desc");
        return articleMapper.selectByExample(articleExample);
    }
}
